/**
 * 
 */
package net.teamix;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import net.teamix.Utils.FacesUtil;

/**
 * @author "Svetoslav Batchovski"
 * 
 */
public class LangElementsControllerBean {

	XMLService xmlService;

	String newLang;

	String newPara;

	ArrayList<SelectItem> emptyLangs = new ArrayList<SelectItem>();

	String selectedLang = "";

	public List<LangElement> getLangElements() {
		return getXmlService().getLangElements();
	}

	public XMLService getXmlService() {
		return xmlService;
	}

	public void setXmlService(XMLService xmlService) {
		this.xmlService = xmlService;
	}

	public String addSprache() {
		String outcome = "failure";
		try {
			getXmlService().addSprache(getSelectedLang());
			outcome = "success";
		} catch (Exception e) {
		}
		return outcome;
	}

	public void addPara(ActionEvent event) {
		String newPara = FacesUtil.getActionAttribute(event, "newValue");
		if (newPara.trim().length() > 0)
			getXmlService().addPara(
					FacesUtil.getActionAttribute(event, "lang"), newPara);
	}

	public void addInc(ActionEvent event) {
		String newPara = FacesUtil.getActionAttribute(event, "newValue");
		if (newPara.trim().length() > 0)
			getXmlService().addInc(FacesUtil.getActionAttribute(event, "lang"),
					newPara);
	}

	public void delPara(ActionEvent event) {
		getXmlService().delPara(FacesUtil.getActionAttribute(event, "lang"),
				FacesUtil.getActionAttribute(event, "delPara"));
	}

	public void moveUpPara(ActionEvent event) {
		getXmlService().moveUpPara(FacesUtil.getActionAttribute(event, "lang"),
				FacesUtil.getActionAttribute(event, "delPara"));
	}

	public void moveDownPara(ActionEvent event) {
		getXmlService().moveDownPara(FacesUtil.getActionAttribute(event, "lang"),
				FacesUtil.getActionAttribute(event, "delPara"));
	}

	public void selectTranslationElement(ActionEvent event) {
		String msgId = FacesUtil.getActionAttribute(event, "transmsgId");
		getRequest().getSession().setAttribute("translationMsgId", msgId);
		if (getXmlService().existsMsgID(msgId))
			return;
		getXmlService().addNewMsgID(msgId);
	}

	private HttpServletRequest getRequest() {
		return (HttpServletRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
	}

	public String nextLangElement() {
		String outcome = "failure";
		try {
			getXmlService().nextLangElement();
			outcome = "success";
		} catch (Exception e) {
		}
		return outcome;
	}

	public String prevLangElement() {
		String outcome = "failure";
		try {
			getXmlService().prevLangElement();
			outcome = "success";
		} catch (Exception e) {
		}
		return outcome;
	}

	public String getNewLang() {
		return newLang;
	}

	public void setNewLang(String newLang) {
		this.newLang = newLang;
	}

	public String getNewPara() {
		return newPara;
	}

	public void setNewPara(String newPara) {
		this.newPara = newPara;
	}

	public ArrayList<SelectItem> getEmptyLangs() {
		return xmlService.getEmptyLangs();
	}

	public void setListLangs(ArrayList<SelectItem> emptyLangs) {
		this.emptyLangs = emptyLangs;
	}

	public String getSelectedLang() {
		return selectedLang;
	}

	public void setSelectedLang(String selectedLang) {
		this.selectedLang = selectedLang;
	}

}
